import { createRouter, createWebHistory } from "vue-router";
import HomeView from "../views/HomeView.vue";
import { startTokenPolling } from "@/utils/token";
const isyydt = import.meta.env.VITE_API_DEV_ENV == "yydt";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  base: process.env.NODE_ENV === "production" ? "/zydt/" : "/", // 加上这一行
  routes: [
    {
      path: "/",
      name: "home",
      redirect: "/login",
      component: HomeView,
      roles: ["entrant"],
    },
    {
      path: "/about",
      name: "about",
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import("../views/AboutView.vue"),
    },
    {
      path: "/login",
      name: "login",
      component: () => import("../views/Login.vue"),
      meta: {
        title: "登录页面",
      },
    },
    {
      path: "/notice",
      name: "notice",
      component: () => import("../views/Notice.vue"),
      meta: {
        roles: ["entrant"],
        title: "考试须知",
      },
    },
    // {
    //   path: "/answerPage",
    //   name: "answerPage",
    //   component: () => import("../views/AnswerPage-zcydt.vue"),
    //   meta: {
    //     title: "答题页面",
    //     roles: ["entrant"],
    //   },
    // },
    {
      path: "/answerPage_zcydt",
      name: "answerPage_zcydt",
      component: () => import("../views/AnswerPage-zcydt.vue"),
      meta: {
        title: "中成药介绍考试",
        roles: ["entrant"],
      },
    },
    {
      path: "/answerPage_yydt",
      name: "answerPage_yydt",
      component: () => import("../views/AnswerPage-yydt.vue"),
      meta: {
        title: "中药处方审核考试",
        roles: ["entrant"],
      },
    },
    {
      path: "/result",
      name: "result",
      component: () => import("../views/Result.vue"),
      meta: {
        title: "考试结果",
        roles: ["entrant"],
      },
    },
    {
      path: "/prescriptio",
      name: "prescriptio",
      component: () => import("../views/Prescriptio.vue"),
      meta: {
        title: "处方列表",
        roles: ["student"],
      },
    },
    {
      path: "/prescriptio-zcydt",
      name: "prescriptio-zcydt",
      component: () => import("../views/Prescriptio-zcydt.vue"),
      meta: {
        title: "处方列表",
        roles: ["student"],
      },
    },
    {
      path: "/trainLog",
      name: "trainLog",
      component: () => import("../views/trainLog.vue"),
      meta: {
        title: "练习记录",
        roles: ["student"],
      },
    },
    {
      path: "/practice",
      name: "practice",
      component: () => import("../views/practice.vue"),
      meta: {
        title: "答题",
        roles: ["student"],
      },
    },
    {
      path: "/login-pra",
      name: "login-pra",
      component: () => import("../views/login-pra.vue"),
      meta: {
        title: "登录",
      },
    },
    {
      path: "/listpage",
      name: "listpage",
      component: () => import("../views/listpage.vue"),
      meta: {
        roles: ["student"],
      },
    },
    {
      path: "/listpage-zcydt",
      name: "listpage-zcydt",
      component: () => import("../views/listpage-zcydt.vue"),
      meta: {
        roles: ["student"],
      },
    },
    {
      path: "/typePage",
      name: "typePage",
      component: () => import("../views/typePage.vue"),
      meta: {
        // title: "选择",
        roles: ["student"],
      },
    },
    {
      path: "/:pathMatch(.*)*",
      name: "404",
      component: () => import("../views/404.vue"),
    },
  ],
});

router.beforeEach((_to, _from, next) => {
  console.log(_to);
  let token = sessionStorage.getItem("api-token");
  let roles = sessionStorage.getItem("ROLES") || "";
  if (!!token) {
    if (!["/login", "/result", "/login-pra"].includes(_to.path)) {
      startTokenPolling();
    }
  }
  if (["/login"].includes(_to.path)) {
    sessionStorage.setItem("ROLES", "entrant");
  } else if (["/login-pra"].includes(_to.path)) {
    sessionStorage.setItem("ROLES", "student");
  }
  if (token || ["/login", "/result", "/login-pra"].includes(_to.path)) {
    if (_to.meta?.roles?.includes(roles) || !_to.meta?.roles) {
      next();
    } else {
      if (["/login", "/result", "/login-pra"].includes(_to.path)) {
        next();
      } else {
        redirectLogin(_to, next);
      }
    }
  } else {
    redirectLogin(_to, next);
  }
  document.title = isyydt ? "中药处方审核模块考试" : "中成药介绍考试";
});
export default router;

const redirectLogin = (_to, next) => {
  let roles = sessionStorage.getItem("ROLES") || "";
  if (roles === "student") {
    next("/login-pra");
  } else {
    next("/login");
  }
};
